# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : base_config_view.py
# Time       ：2023/8/21 18:12
# Author     ：YangYong
# version    ：python 3.10
# Description：
    配置视图类
"""
from flask_restx import fields

from apis.config import conf_ns
from apis.config.models.base_config import BaseConfig
from apis.config.schema.base_config_schema import BaseConfigSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_export_excel_view import BaseExportExcelView
from public.base_response import MarshalModel
from public.base_view import BaseView, BaseDetailsView

conf = {
    'id': fields.String(description='告警ID'),
    'config_name': fields.String(description='配置名称'),
    'config_value': fields.String(description='配置值'),
}
alarm_marshal = MarshalModel('notice', conf_ns)


class ConfigDetailView(BaseDetailsView):
    url = '/detail'

    def __init__(self, *args, **kwargs):
        super(ConfigDetailView, self).__init__(*args, **kwargs)
        self.table_orm = BaseConfig
        self.table_schema = BaseConfigSchema

    @conf_ns.doc(description="获取配置详情")
    @conf_ns.expect(alarm_marshal.detail_request())
    @conf_ns.marshal_with(alarm_marshal.detail_response(conf))
    def get(self):
        """获取配置详情"""
        return super(ConfigDetailView, self).get()


class BaseConfigView(BaseView):
    url = '/'

    def __init__(self, *args, **kwargs):
        super(BaseConfigView, self).__init__(*args, **kwargs)
        self.table_orm = BaseConfig
        self.table_schema = BaseConfigSchema

    @handle_api_error()
    @conf_ns.doc(description='获取配置详情')
    @conf_ns.expect(alarm_marshal.list_request(conf))
    @conf_ns.marshal_with(alarm_marshal.list_response(conf))
    def get(self):
        """获取配置详情"""
        return super(BaseConfigView, self).get()

    @handle_api_error()
    @conf_ns.doc(description='创建配置')
    @conf_ns.expect(alarm_marshal.create_request(conf))
    @conf_ns.marshal_with(alarm_marshal.create_response())
    def post(self):
        """创建配置"""
        return super(BaseConfigView, self).post()

    @handle_api_error()
    @conf_ns.doc(description='修改配置')
    @conf_ns.expect(alarm_marshal.update_request(conf))
    @conf_ns.marshal_with(alarm_marshal.update_response())
    def put(self):
        """修改配置"""
        return super(BaseConfigView, self).put()

    @handle_api_error()
    @conf_ns.doc(description='删除配置')
    @conf_ns.expect(alarm_marshal.delete_request())
    @conf_ns.marshal_with(alarm_marshal.delete_response())
    def delete(self):
        """删除配置"""
        return super(BaseConfigView, self).delete()


class ConfigReportView(BaseExportExcelView):
    url = '/export'

    def __init__(self, *args, **kwargs):
        super(ConfigReportView, self).__init__(*args, **kwargs)
        from datetime import datetime
        self.file_name = f'task_{datetime.now().strftime("%Y-%m-%d")}'
        self.columns = {
            'id': '序号',
            'config_name': '配置名称',
            'config_value': '配置值',
            'creator_name': '创建者',
            'updater_name': '修改者',
            'insert_time': '插入时间',
            'update_time': '更新时间',
        }
        self.is_logger = True
        self.post_action = '导出配置报告'

    @handle_api_error()
    @conf_ns.doc(description='导出配置')
    def post(self):
        """导出配置信息"""
        return super(ConfigReportView, self).post()
